from datetime import datetime, timedelta

from django.core.management.base import BaseCommand

from gis.common import track_logging
from gis.common.telegram import send_text_msg_tele
from itpay.core.agency import agency_service
from itpay.core.conf import conf_client

_LOGGER = track_logging.getLogger(__name__)


class Command(BaseCommand):
    def handle(self, *args, **options):
        _LOGGER.info("begin check complain warning")
        try:
            count, complain = agency_service.list_complaint_agency(
                1,
                1,
                has_done=False,
                created_at_end=datetime.now() - timedelta(minutes=30),
            )
            if count:
                send_text_msg_tele(
                    conf_client.get_value("telegram_group_id"),
                    "当前有{}笔投诉超时未处理，请立即处理。".format(count),
                )
        except Exception:
            _LOGGER.exception("fail execute clear daily receipt address amount")
        _LOGGER.info("end check complain warning")
